package com.chj.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.chj.constants.Constants;
import com.chj.dao.MessageDAO;
import com.chj.entity.Message;
import com.chj.entity.MessageDataGridJson;
import com.chj.service.MessageService;
import com.chj.util.DateUtil;
import com.chj.util.GenerateSequenceUtil;

@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageDAO messageDAO;

    @Override
    public int sendMsg(String userId, String messageMsg) {
        
        Message message = new Message(GenerateSequenceUtil.getUUID(), messageMsg, userId);

        return messageDAO.addMessage(message);
    }

    @Override
    public MessageDataGridJson<Message> adminQuery(String userId,String dateBegin, String dateEnd, String message, int page, int rows) {

        MessageDataGridJson<Message> messageDataGridJson = new MessageDataGridJson<Message>();

        int total = messageDAO.getMessageListCountBy(userId, dateBegin, dateEnd, message);
        List<Message> messageList = messageDAO.getMessageListBy(userId, dateBegin, dateEnd, message, page, rows);

        messageDataGridJson.setTotal(total);
        messageDataGridJson.setRows(messageList);

        return messageDataGridJson;
    }

    @Override
    public int deleteMsg(String messageId) {

        return messageDAO.deleteMessage(messageId);
    }

    @Override
    public JSONObject userQuery(String userId, Date nowDate) {

        JSONObject jsonObject = new JSONObject();

        List<Message> nowRemindList = messageDAO.getMessageListEqualsDateWithUser(userId, DateUtil.dateToString(new Date(), Constants.DATE_FORMAT));
        List<Message> oldRemindList = messageDAO.getMessageListGatterDateWithUser(userId, DateUtil.dateToString(new Date(), Constants.DATE_FORMAT), Constants.SHOW_SIZE);

        jsonObject.put(Constants.NOW_REMIND_LIST, nowRemindList);
        jsonObject.put(Constants.OLD_REMIND_LIST, oldRemindList);

        return jsonObject;
    }

    @Override
    public int getMsgListBy(String userId, String createDateFrom,
            String createDateTo) {
        
        return messageDAO.getMsgListBy(userId
                                     , createDateFrom
                                     , createDateTo);
    }

    @Override
    public List<Message> getMsgListBy(String userId
                                    , String createDateFrom
                                    , String createDateTo
                                    , int page
                                    , int rows) {

        return messageDAO.getMsgListBy(userId
                                     ,createDateFrom
                                     ,createDateTo
                                     ,page
                                     ,rows);
    }

}
